<%@ page language="java" contentType="text/html; charset=utf-8"
    pageEncoding="utf-8"%>
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %>
<!DOCTYPE html>
<html>
<head>
	<title>订单评论</title>
	<meta name="viewport" content="width=device-width, initial-scale=1">
	<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
	<link type="text/css" rel="stylesheet" href="css/bootstrap.css">
	<link type="text/css" rel="stylesheet" href="css/style.css?v=20210222">
	<script type="text/javascript" src="js/jquery.min.js"></script>
	<script type="text/javascript" src="js/bootstrap.min.js"></script>
</head>
<body>

	<jsp:include page="/header.jsp"></jsp:include>
	
	<!--account-->
	<div class="account">
		<div class="container">
			<div class="register">
				<c:if test="${!empty msg }">
					<div class="alert alert-danger">${msg }</div>
				</c:if>
				<div class="register-top-grid">
					<h3>订单评论</h3>
				</div>
				<div class="input">
					<span>总价: </span>
					${order.total}
				</div>
				<form id="addForm"> 
				    <input type="hidden" name="orderId" value="${order.id}">
					<c:forEach items="${order.itemList }" var="item">
							<div class="register-top-grid">
								<input type="hidden" name="goodsId" value="${item.goodsId}">
								<div class="input">
									<span>商品详情: </span>
									<p>${item.goodsName }(${item.price }) x ${item.amount }</p>
								</div>
								<div class="input">
									<span> <label style="color:red;">*</label>评论内容:</span>
									<textarea cols="100" rows="5" id="${item.goodsId}-content" placeholder="请输入评论内容"></textarea>
								</div>
								<div class="clearfix"> </div>
							</div>
					</c:forEach>
				</form>
				<div class="register-but text-center">
				<!-- onclick="document.getElementById('addForm').submit();return false;" -->
						   <a class="btn btn-success" href="#" onclick="submit()">提交</a>
						   &nbsp; &nbsp; &nbsp; &nbsp;&nbsp; &nbsp;
						   <a class="btn btn-warning" href="${pageContext.request.contextPath }/order_list">退出</a>
						   <div class="clearfix"> </div>
				</div>
				<div class="clearfix"> </div>
			</div>
	    </div>
	</div>
	<!--//account-->

	<jsp:include page="/footer.jsp"></jsp:include>

	<script type="text/javascript">
       function submit() {
    	   var params = $("#addForm").serialize();
    	   params = decodeURIComponent(params,true);
    	   var list = params.split("&");
    	   var formData = [];
    	   var orderId = 0;
    	   list.forEach(function(param){
    		   var formList = param.split("=");
    		   if(formList[0]==="goodsId") {
    			   var item = {
    	    		   "id": 0,
    				   "goodsId": formList[1],
    				   "content": $("#"+formList[1]+"-content").val()? $("#"+formList[1]+"-content").val():"该用户未评论",
    	    		   "orderId": orderId,
    	    		   "parentId": 0,
    	    		   "createdTime": '',
    	    		   "createdTime": '',
    	    		   "updatedTime": '',
    	    		   "toUserId": 0,
    	    		   "creatorId": 0
    			   
    			   }
    			   formData.push(item);
    			   
    		   }else if(formList[0]==="orderId"){
    			   orderId = formList[1];
    		   }
    	   })
    	   console.log(JSON.stringify(formData));
           //params = encodeURI(encodeURI(params));
    	   $.ajax({
    		    type : "POST",
    		    contentType : 'application/json',
    		    url : '<%=request.getContextPath()%>/OrderAddevaluation',
    		    data: JSON.stringify({
    		    	"orderId": orderId,
    		    	"evaluations": formData,
    		    }),
    		    async :false,
    		    dataType : "json",
    		    success : function(data) { 
    		    	window.location.href=<%=request.getContextPath()%>+"/order_list";
    		    },
    		    error:function(){
    		    	window.location.href=<%=request.getContextPath()%>+"/order_list";
    		    }

    		   });
       }
	</script>
</body>
</html>